package com.ll.beautymall.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ll.beautymall.R;
import com.ll.beautymall.bean.HomeProduct;
import com.ll.beautymall.ui.home.product.ProductActivity;
import com.ll.beautymall.widget.CircleTransform;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/22.
 */
public class ContentGvAdapter extends RecyclerView.Adapter<ContentGvAdapter.ViewHolder> {//BaseAdapter
    private List<HomeProduct> products;
    private Context mContext;

    public ContentGvAdapter(Context context) {
        mContext = context;
    }

    public void setData(List<HomeProduct> list) {
        this.products = list;
        notifyDataSetChanged();
    }

    public void addData(List<HomeProduct> list) {
        this.products.addAll(list);
        notifyDataSetChanged();
    }

    public void clear() {
        if (products != null && products.size() > 0) {
            this.products.clear();
            notifyDataSetChanged();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.home_gv_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        HomeProduct product = products.get(position);
        Picasso.with(mContext).load(product.getPic_url()).transform(new CircleTransform()).into(holder.homeGvItemIvCover);
        holder.homeGvItemTvTitle.setText(product.getTitle());
        holder.homeGvItemTvPrice.setText("￥" + product.getCoupon_price());
        holder.homeGvItemTvSale.setText(product.getSales_volume() + "件已售");

        //当点击某一商品时跳到 详情页
        holder.homeGvItemIvCover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HomeProduct homeProduct = products.get(position);
                Intent intent = new Intent(mContext, ProductActivity.class);
                intent.putExtra("product", homeProduct);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return products != null ? products.size() : 0;
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.home_gv_item_iv_cover)
        ImageView homeGvItemIvCover;
        @BindView(R.id.home_gv_item_tv_title)
        TextView homeGvItemTvTitle;
        @BindView(R.id.home_gv_item_tv_price)
        TextView homeGvItemTvPrice;
        @BindView(R.id.home_gv_item_tv_sale)
        TextView homeGvItemTvSale;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
